unit ec_changegold;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Buttons,
  StdCtrls, Spin, EditBtn, ec_base;

type

  { TECChangeGoldFrame }

  TECChangeGoldFrame = class(TECBaseFrame)
    VariableEditButton: TEditButton;
    DecreaseRadioButton: TRadioButton;
    ConstantRadioButton: TRadioButton;
    VariableRadioButton: TRadioButton;
    OperationGroupBox: TGroupBox;
    IncreaseRadioButton: TRadioButton;
    AmountGroupBox: TGroupBox;
    ConstantSpinEdit: TSpinEdit;
    procedure AmountRadioButtonChange(Sender: TObject);
    procedure VariableEditButtonButtonClick(Sender: TObject);
    procedure VariableEditButtonKeyPress(Sender: TObject; var Key: char);
  private
    VariableId: Integer;
    procedure UpdateVariableName;
    procedure UpdateStrings;
  public
    procedure InitNew; override;
    procedure InitExisting; override;
    procedure MakeResultingCommand; override;
  end;

var
  ECChangeGoldFrame: TECChangeGoldFrame;

resourcestring
  rcChangeGoldTitle = 'Change %s (currency)';
  rcChangeGoldTitle_NoCurrency = 'Change currency';

implementation

uses
  namedindexselection, fpjson, constants;

{$R *.lfm}

{ TECChangeGoldFrame }

procedure TECChangeGoldFrame.AmountRadioButtonChange(Sender: TObject);
begin
  ConstantSpinEdit.Enabled := ConstantRadioButton.Checked;
  VariableEditButton.Enabled := VariableRadioButton.Checked;
  if VariableRadioButton.Checked then
     UpdateVariableName
end;

procedure TECChangeGoldFrame.VariableEditButtonButtonClick(Sender: TObject);
begin
  VariableId := NamedIndexSelectionForm.SelectVariable(VariableId, Db);
  UpdateVariableName
end;

procedure TECChangeGoldFrame.VariableEditButtonKeyPress(Sender: TObject;
  var Key: char);
begin
  Key := #0;
  VariableEditButtonButtonClick(Sender)
end;

procedure TECChangeGoldFrame.UpdateVariableName;
begin
  VariableEditButton.Text := Db.GetVariableName(VariableId)
end;

procedure TECChangeGoldFrame.UpdateStrings;
var
  Currency: String = '';
begin
  if Db <> nil then
     Currency := Trim(Db.System.Strings['currencyUnit']);

  if Currency = '' then
     Caption := rcChangeGoldTitle_NoCurrency
  else
    Caption := rcChangeGoldTitle.Format([Currency]);
end;

procedure TECChangeGoldFrame.InitNew;
begin
  UpdateStrings;
  VariableId := 1;
  AmountRadioButtonChange(Self)
end;

procedure TECChangeGoldFrame.InitExisting;
var
  Params: TJSONArray = nil;
  ActionCode: Integer = 0;
  AmountType: Integer = 0;
  AmountValue: Integer = 0;
begin
  UpdateStrings;
  VariableId := 1;

  Params := GetFirstParams;

  if (Params <> nil) and (Params.Count >= 1) and (Params[0].JSONType = jtNumber)
  then
    ActionCode := Params[0].AsInteger;

  if ActionCode = 0 then
    IncreaseRadioButton.Checked := True
  else
    DecreaseRadioButton.Checked := True;

  if (Params <> nil) and (Params.Count >= 2) and (Params[1].JSONType = jtNumber)
  then
    AmountType := Params[1].AsInteger;

  if AmountType = 0 then
    ConstantRadioButton.Checked := True
  else
    VariableRadioButton.Checked := True;
  AmountRadioButtonChange(Self);

  if (Params <> nil) and (Params.Count >= 3) and (Params[2].JSONType = jtNumber)
  then
    AmountValue := Params[2].AsInteger;

  if AmountType = 0 then
    ConstantSpinEdit.Value := AmountValue
  else begin
    VariableId := AmountValue;
    UpdateVariableName
  end
end;

procedure TECChangeGoldFrame.MakeResultingCommand;
  function Action: Integer;
  begin
    if IncreaseRadioButton.Checked then
       Action := 0
    else
      Action := 1
  end;

  function AmountType: Integer;
  begin
    if ConstantRadioButton.Checked then
      AmountType := 0
    else
      AmountType := 1
  end;

  function AmountValue: Integer;
  begin
    if ConstantRadioButton.Checked then
      AmountValue := ConstantSpinEdit.Value
    else
      AmountValue := VariableId
  end;
begin
  ResultingCommand := SingleLineResultingCommand(
    CHANGE_GOLD_EC_CODE,
    TJSONArray.Create([Action, AmountType, AmountValue])
  );
end;

end.

